package com.epalmpay.service.weixin.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.epalmpay.bean.ShopDetailBean;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.YunStreamEnum;
import com.epalmpay.mapper.*;
import com.epalmpay.service.weixin.IWxApiFindShopService;
import com.epalmpay.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("wxApiFindShopService")
public class WxApiFindShopServiceImpl implements IWxApiFindShopService {

	@Resource
	private ShopMapper shopMapper;
	@Resource
	private ActivityUnroutineMapper activityUnroutineMapper;
	@Resource
	private MallGoodMapper mallGoodMapper;
	@Resource
	private MallGoodCommentMapper mallGoodCommentMapper;
	@Resource
	private TradeOfflineMapper tradeOfflineMapper;

	@Resource
	private MemberCardMapper memberCardMapper;

	@Resource
	private AgentMapper agentMapper;


	@Override
	public Map<String, Object> findDiscountShopList( String groupId,String memberId,String user_longitude,String user_latitude,
													 Integer distance, String name,Integer goodCategory,Integer sortMethod,
													 Integer pageNo,Integer pageSize)throws Exception {

		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		Map<String, Object> map = new HashMap<String, Object>();

		Map<String,Object> param=new HashMap<String,Object>();
		//取得门店数据；
		param.clear();
		param.put("groupId", Long.parseLong(groupId));
		param.put("name", name);
		param.put("goodCategory", goodCategory);

		param.put("start", pageSize*(pageNo-1));
		param.put("limit", pageSize);

		List<Shop> shops = null;

		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)){
			param.put("distance", distance);
			param.put("user_latitude", user_latitude);
			param.put("user_longitude", user_longitude);
			param.put("sortMethod", sortMethod);
			shops = shopMapper.selectDiscountShopWithCoordinate(param);
		}else{
			shops = shopMapper.selectDiscountShop(param);
		}

        if(shops == null)
        	return map;

		List<Map<String,Object>> shopList = new ArrayList<Map<String, Object>>();
		for (Shop shop:shops) {
			Map<String, Object> shopDetail = new HashMap<String, Object>();
				shopDetail.put("shop_id", shop.getId());
				shopDetail.put("shop_ico",shop.getIcon());
				shopDetail.put("shop_name",shop.getMerchantName());
				shopDetail.put("shop_score",shop.getShopScore());
			    shopDetail.put("sale_num",shop.getSaleNum());
//				shopDetail.put("shop_type",shop.getIndustryName());
			    shopDetail.put("shop_location",shop.getDistance());

				//取得门店活动列表
				List<ActivityUnroutine> activityUnroutines = activityUnroutineMapper.queryActivityListByShopID(shop.getId());
				List<Map<String,Object>> discountList = new ArrayList<Map<String, Object>>();
				for(ActivityUnroutine activityUnroutine:activityUnroutines){
					Map<String, Object> discountDetail = new HashMap<String, Object>();
					discountDetail.put("aitiviate_id",activityUnroutine.getActivityId());
					discountDetail.put("aitiviate_type",activityUnroutine.getActivityType());
					discountDetail.put("aitiviate_name",activityUnroutine.getActivityName());
					discountList.add(discountDetail);
				}
				if(discountList.size()>0) {
					shopDetail.put("discount_list", discountList);
				}
				//取门店活动最后一个
				ActivityUnroutine  activityUnroutine=activityUnroutineMapper.queryActivityShopID(shop.getId());
				if(activityUnroutine !=null){
					shopDetail.put("shop_type",activityUnroutine.getActivityName());
				}else{
					shopDetail.put("shop_type","");
				}

			shopList.add(shopDetail);
		}
		if(shopList.size()>0) {
			map.put("shop_list", shopList);
		}
		return map;
	}

	public Map<String, Object> getShopDiscountDetail(String groupId,String memberId,String user_longitude,String user_latitude,String shopId) throws Exception{
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(shopId)) {
			throw new ApplicationException(Rescode.FAIL,"shopId 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//取得门店信息
		Shop shop = shopMapper.selectDiscountShopDetail(Long.parseLong(shopId));
		map.put("shop_id",shop.getId());
		map.put("shop_ico",shop.getIcon());
		map.put("shop_name",shop.getMerchantName());
		map.put("shop_score",shop.getShopScore());
		//map.put("shop_consume_average",);
		map.put("shop_type",shop.getIndustryName());
		//距离计算
		String tolongitude = shop.getLongitude();
		String tolatitude = shop.getLatitude();
		long calDistance = -1;


		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
				&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){
//
//			calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_longitude,user_latitude,tolatitude,tolongitude);
			calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_latitude,user_longitude,tolatitude,tolongitude);
			if(calDistance>0){
				map.put("shop_location",calDistance);
			}else{
				double s = LocationUtils.getDistance(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
				map.put("shop_location",(long)s);
			}
		}else{
			map.put("shop_location",calDistance);
		}




		map.put("longitude",shop.getLongitude());
		map.put("latitude",shop.getLatitude());
//		map.put("shop_location",calDistance);
		map.put("shop_address",shop.getAddress());
		map.put("address_district",shop.getAddressDistrict());
		map.put("shop_phone",shop.getPhone());
		map.put("work_time",shop.getWorkTime());
		map.put("shop_meno",shop.getMemo());

        //门店优惠商品列表
		List<MallGood> mallGoods = mallGoodMapper.selectShopDiscountGoodList(Long.parseLong(shopId));
		List<Map<String,Object>> goodList = new ArrayList<Map<String, Object>>();
		for (MallGood mallGood:mallGoods){
			Map<String, Object> goodMap = new HashMap<String, Object>();
			goodMap.put("good_id",mallGood.getId());
			goodMap.put("good_icon",mallGood.getIconPicture());
			goodMap.put("good_name",mallGood.getName());
			goodMap.put("sale_price",mallGood.getSalePrice());
			goodMap.put("promotion_price",mallGood.getPromotionPrice());
			goodMap.put("sale_num",mallGood.getSaleNum());
			goodList.add(goodMap);
		}
		if(goodList.size()>0) {
			map.put("good_list", goodList);
		}
		//取得门店活动列表
		List<ActivityUnroutine> activityUnroutines = activityUnroutineMapper.queryActivityListByShopID(shop.getId());
		List<Map<String,Object>> discountList = new ArrayList<Map<String, Object>>();
		for(ActivityUnroutine activityUnroutine:activityUnroutines){
			Map<String, Object> discountDetail = new HashMap<String, Object>();
			discountDetail.put("aitiviate_id",activityUnroutine.getActivityId());
			discountDetail.put("aitiviate_type",activityUnroutine.getActivityType());
			discountDetail.put("aitiviate_name",activityUnroutine.getActivityName());
			discountList.add(discountDetail);
		}
		if(discountList.size()>0) {
			map.put("activity_list", discountList);
		}
        //取得门店评价列表
		Map<String,Object> param=new HashMap<String,Object>();
		param.clear();
		param.put("groupId", Long.parseLong(groupId));
		param.put("shopId", Long.parseLong(shopId));
		param.put("goodId", null);
		param.put("auditStatus", 1);
		param.put("start", 0);
		param.put("limit", 2);//取2行
		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectShopOrGoodComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			if(mallGoodComment.getAuditStatus() == 1){
				goodCommentMap.put("comment_id",mallGoodComment.getId());
				if(mallGoodComment.getIsCryptonym()){
					goodCommentMap.put("member_icon","");
					goodCommentMap.put("member_name","匿名用户");
				}
				else{
					goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
					goodCommentMap.put("member_name",mallGoodComment.getMemberName());
				}
				goodCommentMap.put("score",mallGoodComment.getCommentLevel());
				goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfTime));
				goodCommentMap.put("comment_content",mallGoodComment.getContent());
				goodCommentMap.put("pic_urls",mallGoodComment.getPicUrls());
				goodCommentMap.put("shop_id",mallGoodComment.getShopId());
				goodCommentMap.put("shop_name",mallGoodComment.getShopName());
				commentList.add(goodCommentMap);
			}
		}
		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}else{
			map.put("comment_list", commentList);
		}

		return map;
    }

	public Map<String, Object> getDiscountGoodDetail(String groupId,String memberId,
													 String user_longitude,String user_latitude,String goodId,String shopId) throws Exception{
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(goodId)) {
			throw new ApplicationException(Rescode.FAIL,"goodId 不能为空！");
		}
		if (Tools.isEmpty(shopId)) {
			throw new ApplicationException(Rescode.FAIL,"shopId 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//取得商品信息
		MallGood mallGood = mallGoodMapper.selectGoodDetailById(Long.parseLong(goodId));
		map.put("good_id",mallGood.getId());
		map.put("good_icon",mallGood.getIconPicture());
		map.put("good_picture",mallGood.getPicture());
		map.put("good_name",mallGood.getName());
		map.put("sale_price",mallGood.getSalePrice());
		map.put("promotion_price",mallGood.getPromotionPrice());
		map.put("sale_num",mallGood.getSaleNum());
		map.put("is_anytime_refund",mallGood.getIsAnytimeRefund());
		map.put("is_auto_refund",mallGood.getIsAutoRefund());
		map.put("comment_num",mallGood.getCommentNum());
		map.put("good_comment_rate",AmountUtils.devide(mallGood.getGoodCommentNum(),mallGood.getCommentNum()));
        map.put("limit_number",mallGood.getLimitNumber());
		map.put("valid_date",DateUtil.fomatDate(mallGood.getValidDate(),DateUtil.sdfTime));
		map.put("attribute",mallGood.getAttribute());

		//取得门店信息
		Shop shop = shopMapper.selectDiscountShopDetail(Long.parseLong(shopId));
		map.put("shop_id",shop.getId());
		map.put("allshop_num",StringUtil.splitCount(mallGood.getCanUsedShop()));
		map.put("shop_name",shop.getMerchantName());
		map.put("shop_score",shop.getShopScore());
		//map.put("shop_consume_average",);
		map.put("shop_type",shop.getIndustryName());
		//距离计算
		String tolongitude = shop.getLongitude();
		String tolatitude = shop.getLatitude();
		long calDistance = -1;
		/*if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
				&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){
			calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_longitude,user_latitude,tolongitude,tolatitude);
		}*/


		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
				&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){
//
//			calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_longitude,user_latitude,tolatitude,tolongitude);
			calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_latitude,user_longitude,tolatitude,tolongitude);
			if(calDistance>0){
				map.put("shop_location",calDistance);
			}else{
				double s = LocationUtils.getDistance(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
				map.put("shop_location",(long)s);
			}
		}else{
			map.put("shop_location",calDistance);
		}
//		map.put("shop_location",calDistance);
		map.put("shop_address",shop.getAddress());
		map.put("shop_phone",shop.getPhone());

		//门店优惠商品列表(去掉当前商品)
		List<MallGood> mallGoods = mallGoodMapper.selectShopDiscountGoodList(Long.parseLong(shopId));
		List<Map<String,Object>> goodList = new ArrayList<Map<String, Object>>();
		for (MallGood mallGoodrec:mallGoods){
			Map<String, Object> goodMap = new HashMap<String, Object>();
			goodMap.put("good_id",mallGoodrec.getId());
			goodMap.put("good_icon",mallGoodrec.getIconPicture());
			goodMap.put("good_name",mallGoodrec.getName());
			goodMap.put("sale_price",mallGoodrec.getSalePrice());
			goodMap.put("promotion_price",mallGoodrec.getPromotionPrice());
			goodMap.put("sale_num",mallGoodrec.getSaleNum());
			goodList.add(goodMap);
		}
		if(goodList.size()>0) {
			map.put("good_list", goodList);
		}
		//取得商品评价列表
		Map<String,Object> param=new HashMap<String,Object>();
		param.clear();
		param.put("shopId", null);
		param.put("goodId", Long.parseLong(goodId));
		param.put("start", 0);
		param.put("limit", 2);//取2行
		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectShopOrGoodComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			goodCommentMap.put("comment_id",mallGoodComment.getId());
			goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
			goodCommentMap.put("member_name",mallGoodComment.getMemberName());
			goodCommentMap.put("score",mallGoodComment.getCommentLevel());
			goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfTime));
			goodCommentMap.put("comment_content",mallGoodComment.getContent());
			commentList.add(goodCommentMap);
		}
		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}

		return map;
	}

	public Map<String, Object> getGoodPictureDetail(String memberId,String goodId) throws Exception{
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(goodId)) {
			throw new ApplicationException(Rescode.FAIL,"goodId 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		MallGood mallGood = mallGoodMapper.selectByPrimaryKey(Long.parseLong(goodId));
		map.put("attribute",mallGood.getAttribute());
		map.put("picture",mallGood.getPicture());
		map.put("content",mallGood.getContent());

		return map;
	}

	public Map<String, Object> getGoodCommentList(String goodId,Integer auditStatus ,Integer starLevel,Integer pageNo,Integer pageSize ) throws Exception{
		if (Tools.isEmpty(goodId)) {
			throw new ApplicationException(Rescode.FAIL,"goodId 不能为空！");
		}
		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//取得商品评价列表
		Map<String,Object> param=new HashMap<String,Object>();
		param.clear();
		param.put("shopId", null);
		param.put("goodId", Long.parseLong(goodId));
		param.put("auditStatus", auditStatus);
		param.put("commentLevel", starLevel);
		param.put("start", (pageNo -1)*pageSize);
		param.put("limit", pageSize);
		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectShopOrGoodComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			goodCommentMap.put("comment_id",mallGoodComment.getId());
			goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
			goodCommentMap.put("member_name",mallGoodComment.getMemberName());
			goodCommentMap.put("score",mallGoodComment.getCommentLevel());
			goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfTime));
			goodCommentMap.put("comment_content",mallGoodComment.getContent());
			commentList.add(goodCommentMap);
		}
		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}

		return map;
	}

	public Map<String, Object> getShopCommentList(String shopId,Integer auditStatus ,Integer starLevel,Integer pageNo,Integer pageSize ) throws Exception{
		if (Tools.isEmpty(shopId)) {
			throw new ApplicationException(Rescode.FAIL,"shopId 不能为空！");
		}
		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//取得商品评价列表
		Map<String,Object> param=new HashMap<String,Object>();
		param.clear();
		param.put("shopId", null);
		param.put("shopId", Long.parseLong(shopId));
		param.put("auditStatus", auditStatus);
		param.put("commentLevel", starLevel);
		param.put("start", (pageNo -1)*pageSize);
		param.put("limit", pageSize);
		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectShopOrGoodComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			goodCommentMap.put("comment_id",mallGoodComment.getId());
			goodCommentMap.put("score",mallGoodComment.getCommentLevel());
			goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfTime));
			goodCommentMap.put("comment_content",mallGoodComment.getContent());
			if(mallGoodComment.getIsCryptonym()){
				goodCommentMap.put("member_icon","");
				goodCommentMap.put("member_name","匿名用户");
			}
			else{
				goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
				goodCommentMap.put("member_name",mallGoodComment.getMemberName());
			}
			goodCommentMap.put("pic_urls",mallGoodComment.getPicUrls());
			goodCommentMap.put("shop_id",mallGoodComment.getShopId());
			goodCommentMap.put("shop_name",mallGoodComment.getShopName());
			commentList.add(goodCommentMap);
		}
		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}

		return map;
	}

	public Map<String, Object>  getGoodAllShopList(String goodId,String user_longitude,String user_latitude)  throws Exception{
		if (Tools.isEmpty(goodId)) {
			throw new ApplicationException(Rescode.FAIL,"goodId 不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<Shop> shops = shopMapper.selectGoodAllShop(Long.parseLong(goodId));
		List<Map<String,Object>> shopList = new ArrayList<Map<String, Object>>();
		for (Shop shop:shops) {
			Map<String, Object> shopDetail = new HashMap<String, Object>();
			shopDetail.put("shop_id", shop.getId());
			shopDetail.put("shop_ico", shop.getIcon());
			shopDetail.put("shop_name", shop.getMerchantName());
			shopDetail.put("shop_score", shop.getShopScore());
			//shopDetail.put("shop_consume_average",shop.getShopScore());
			shopDetail.put("shop_type", shop.getIndustryName());
			String tolongitude = shop.getLongitude();
			String tolatitude = shop.getLatitude();
			long calDistance = -1;
			/*if (StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
					&& StringUtil.notEmpty(tolongitude) && StringUtil.notEmpty(tolatitude)) {
				calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING, user_longitude, user_latitude, tolongitude, tolatitude);
			}*/

			if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
					&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){
//
//				calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_longitude,user_latitude,tolatitude,tolongitude);
				calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,user_latitude,user_longitude,tolatitude,tolongitude);
					if(calDistance>0){
					map.put("shop_location",calDistance);
				}else{
					double s = LocationUtils.getDistance(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
					map.put("shop_location",(long)s);
				}
			}else{
				map.put("shop_location",calDistance);
			}
//			shopDetail.put("shop_location", calDistance);
			shopDetail.put("shop_address",shop.getAddress());
			shopDetail.put("address_district",shop.getAddressDistrict());
			shopList.add(shopDetail);
		}
		if(shopList.size()>0) {
			map.put("shop_list", shopList);
		}

		return map;
	}



	/**
	 * sql排序
	 * @param groupId
	 * @param memberId
	 * @param user_longitude
	 * @param user_latitude
	 * @param address_province
	 * @param distance
	 * @param name
	 * @param goodCategory
	 * @param ysServiceType
	 * @param ysOrderBy
	 * @param pageNo
	 * @param pageSize
	 * @param province
	 * @param city
	 * @param district
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> findDiscountCarShopList2(String groupId, String memberId, String user_longitude,
													   String user_latitude,String address_province, Integer distance, String name, Integer goodCategory,
													   Integer ysServiceType,Integer ysOrderBy, Integer pageNo, Integer pageSize,String province,String city ,String district) throws Exception {
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}
		Map<String,Object> param=new HashMap<String,Object>();
		Map<String, Object> stringObjectMap = new HashMap<>();
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)) {
			//坐标转换
			stringObjectMap = QqMapApiHelper.Convert_BD09_To_GCJ02(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude));
			String longitude = stringObjectMap.get("toLongitude").toString();
			String latitude = stringObjectMap.get("toLatitude").toString();
			param.put("user_latitude",latitude);
			param.put("user_longitude",longitude);
			
		}
		Map<String, Object> map = new HashMap<String, Object>();

		
		//取得门店数据；
		param.put("subIndustry",51);
		param.put("groupId", Long.parseLong(groupId));
		param.put("ysServiceType",ysServiceType);
		param.put("name", name);
		param.put("ysOrderBy", ysOrderBy);
	
		if(address_province !=null && StringUtil.notEmpty(address_province)){
			param.put("addressProvince",address_province);
		}else{
			param.put("province", province);
			param.put("city", city);
			//param.put("district", district);数量较少 先不过滤区
		}


		int sumCount = shopMapper.selectDiscountCarShopCount(param);
		if(sumCount<=0)
			return map;

		param.put("start", pageSize*(pageNo-1));
		param.put("limit", pageSize);
		System.out.print("参数:"+JSONObject.toJSONString(param));
		List<Shop> shops=null;
		
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)){
			
			shops = shopMapper.selectDiscountCarShop2(param);
		}else{
			shops = shopMapper.selectDiscountCarShop(param);
		}
		 
		if(shops == null)
			return map;

//		long calDistance;
		List<ShopDetailBean> shopList = new ArrayList<ShopDetailBean>();

		for (Shop shop:shops) {
			   ShopDetailBean shopDetail = new ShopDetailBean();
				if (shop.getAlbumpics() != null) {
					String[] photos = shop.getAlbumpics().split(",");
					shopDetail.setShop_ico(photos[0]);
				}

				shopDetail.setShop_id(shop.getId());
				shopDetail.setShop_name(shop.getMerchantName());

				if (shop.getShopScore() != null) {
					shopDetail.setShop_score( shop.getShopScore());
				} else {
					shopDetail.setShop_score( "5");
				}
				shopDetail.setSale_num(shop.getSaleNum());
				if (shop.getShopType() != null) {
					shopDetail.setShop_type( shop.getShopType());
				} else {
					shopDetail.setShop_type( -1);
				}

				shopDetail.setAddress_province(shop.getAddressProvince());
				shopDetail.setAddress_city( shop.getAddressCity());
				shopDetail.setAddress_district(shop.getAddressDistrict());
				shopDetail.setShop_address(shop.getAddress());

				//tx转百度 返回回去
//				Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLongitude()), Double.parseDouble(shop.getLatitude()));
				Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLatitude()), Double.parseDouble(shop.getLongitude()));
				//坐标转换
				double longitude2 = Double.parseDouble(stringObjectMap2.get("toLongitude").toString());
				double latitude2 = Double.parseDouble(stringObjectMap2.get("toLatitude").toString());
				shopDetail.setTolatitude( latitude2);
				shopDetail.setTolongitude( longitude2);

				if(shop.getDistance()!=null){
					 shopDetail.setShop_location(shop.getDistance().longValue());
				}else{
					if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)){						
						String longitude = stringObjectMap.get("toLongitude").toString();
						String latitude = stringObjectMap.get("toLatitude").toString();
						String tolatitude=shopDetail.getTxTolatitude();
						String tolongitude=shopDetail.getTxTolongitude();
						 double distance2= LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));					
						 shopDetail.setShop_location((long)distance2);
					}else{
						 shopDetail.setShop_location((long)-1);
					}
					
				}
				
				 
				/**
				 * 动态返回给前端
				 */
				List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();

				if (shop.getIsInstall() != null && shop.getIsInstall() == 1 && shop.getInstallText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Install.getName(), shop.getIsInstall(), shop.getInstallText(), shop.getInstallText().split(",").length));
				}
				if (shop.getIsRefit() != null && shop.getIsRefit() == 1 && shop.getRefitText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Refit.getName(), shop.getIsRefit(), shop.getRefitText(), shop.getRefitText().split(",").length));
				}
				if (shop.getIsCosmetology() != null && shop.getIsCosmetology() == 1 && shop.getCosmetologyText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Cosmetology.getName(), shop.getIsCosmetology(), shop.getCosmetologyText(), shop.getCosmetologyText().split(",").length));
				}
				if (shop.getIsRepair() != null && shop.getIsRepair() == 1 && shop.getRepairText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Repair.getName(), shop.getIsRepair(), shop.getRepairText(), shop.getRepairText().split(",").length));
				}
				if (shop.getIsMaintain() != null && shop.getIsMaintain() == 1 && shop.getMaintainText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Maintain.getName(), shop.getIsMaintain(), shop.getMaintainText(), shop.getMaintainText().split(",").length));
				}
				if (typeList.size() > 0) {
					shopDetail.setType_list( typeList);
				} else {
					shopDetail.setType_list( null);
				}
				shopDetail.setTxTolongitude( shop.getLongitude());
				shopDetail.setTxTolatitude(shop.getLatitude());
				shopList.add(shopDetail);

		}	
		
		
		map.put("shop_list", shopList);
		return map;
	}
	
	
	@Override
	public Map<String, Object> findDiscountCarShopList(String groupId, String memberId, String user_longitude,
													   String user_latitude,String address_province, Integer distance, String name, Integer goodCategory,
													   Integer ysServiceType,Integer ysOrderBy, Integer pageNo, Integer pageSize,String province,String city ,String district) throws Exception {
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		Map<String, Object> stringObjectMap = new HashMap<>();
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)) {
			//坐标转换
			stringObjectMap = QqMapApiHelper.Convert_BD09_To_GCJ02(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude));
		}
		Map<String, Object> map = new HashMap<String, Object>();

		Map<String,Object> param=new HashMap<String,Object>();
		//取得门店数据；
		param.clear();
		param.put("subIndustry",51);
		param.put("groupId", Long.parseLong(groupId));
		param.put("ysServiceType",ysServiceType);
		param.put("name", name);
		param.put("ysOrderBy", ysOrderBy);
		if(address_province !=null && StringUtil.notEmpty(address_province)){
			param.put("addressProvince",address_province);
		}else{
			param.put("province", province);
			param.put("city", city);
			//param.put("district", district);数量较少 先不过滤区
		}
		System.out.print("参数:"+JSONObject.toJSONString(param));
		List<Shop> shops = shopMapper.selectDiscountCarShop(param);
		if(shops == null)
			return map;

//		long calDistance;
		List<ShopDetailBean> shopList = new ArrayList<ShopDetailBean>();

		for (Shop shop:shops) {
			ShopDetailBean shopDetail = new ShopDetailBean();

				if (shop.getAlbumpics() != null) {
					String[] photos = shop.getAlbumpics().split(",");
					shopDetail.setShop_ico(photos[0]);
				}

				shopDetail.setShop_id(shop.getId());
				shopDetail.setShop_name(shop.getMerchantName());

				if (shop.getShopScore() != null) {
					shopDetail.setShop_score( shop.getShopScore());
				} else {
					shopDetail.setShop_score( "5");
				}
				shopDetail.setSale_num(shop.getSaleNum());
				if (shop.getShopType() != null) {
					shopDetail.setShop_type( shop.getShopType());
				} else {
					shopDetail.setShop_type( -1);
				}

				shopDetail.setAddress_province(shop.getAddressProvince());
				shopDetail.setAddress_city( shop.getAddressCity());
				shopDetail.setAddress_district(shop.getAddressDistrict());
				shopDetail.setShop_address(shop.getAddress());

				//tx转百度 返回回去
//				Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLongitude()), Double.parseDouble(shop.getLatitude()));
				Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLatitude()), Double.parseDouble(shop.getLongitude()));
				//坐标转换
				double longitude2 = Double.parseDouble(stringObjectMap2.get("toLongitude").toString());
				double latitude2 = Double.parseDouble(stringObjectMap2.get("toLatitude").toString());
				shopDetail.setTolatitude( latitude2);
				shopDetail.setTolongitude( longitude2);

				/**
				 * 动态返回给前端
				 */
				List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();

				if (shop.getIsInstall() != null && shop.getIsInstall() == 1 && shop.getInstallText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Install.getName(), shop.getIsInstall(), shop.getInstallText(), shop.getInstallText().split(",").length));
				}
				if (shop.getIsRefit() != null && shop.getIsRefit() == 1 && shop.getRefitText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Refit.getName(), shop.getIsRefit(), shop.getRefitText(), shop.getRefitText().split(",").length));
				}
				if (shop.getIsCosmetology() != null && shop.getIsCosmetology() == 1 && shop.getCosmetologyText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Cosmetology.getName(), shop.getIsCosmetology(), shop.getCosmetologyText(), shop.getCosmetologyText().split(",").length));
				}
				if (shop.getIsRepair() != null && shop.getIsRepair() == 1 && shop.getRepairText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Repair.getName(), shop.getIsRepair(), shop.getRepairText(), shop.getRepairText().split(",").length));
				}
				if (shop.getIsMaintain() != null && shop.getIsMaintain() == 1 && shop.getMaintainText() != null) {
					typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Maintain.getName(), shop.getIsMaintain(), shop.getMaintainText(), shop.getMaintainText().split(",").length));
				}
				if (typeList.size() > 0) {
					shopDetail.setType_list( typeList);
				} else {
					shopDetail.setType_list( null);
				}
				shopDetail.setTxTolongitude( shop.getLongitude());
				shopDetail.setTxTolatitude(shop.getLatitude());
				shopList.add(shopDetail);

		}
		if (StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)) {
			String longitude = stringObjectMap.get("toLongitude").toString();
			String latitude = stringObjectMap.get("toLatitude").toString();
           finishgetDistance(shopList,longitude,latitude);
           ysOrderBy=1;
		} else {
			ysOrderBy=-1;

		}
		
		//有距离才可以排序
		if(ysOrderBy!=null && ysOrderBy==1  ){

			shopList = SortUtil.sort(shopList, "shop_location", "asc");
		}

		param.put("start", pageSize*(pageNo-1));
		param.put("limit", pageSize);
		if(shopList.size()>0) {
			List<ShopDetailBean> result = new ArrayList<ShopDetailBean>();
			for(int i=pageSize*(pageNo-1);i<shopList.size()&&i<pageSize*pageNo;i++){
				result.add(shopList.get(i));
			}
			map.put("shop_list", result);
			System.out.print("shop_list::"+result.size() +"...shopList:::"+shopList.size());
		}

		return map;
	}

	@Override
	public Map<String, Object> findDiscountCarShopMapList(String groupId, String memberId, String user_longitude,
													   String user_latitude, Integer distance) throws Exception {
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL, "groupId 不能为空！");
		}

		Map<String, Object> stringObjectMap = new HashMap<>();
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)) {
			//坐标转换
			stringObjectMap = QqMapApiHelper.Convert_BD09_To_GCJ02(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude));
		}
		Map<String, Object> map = new HashMap<String, Object>();

		Map<String,Object> param=new HashMap<String,Object>();
		//取得门店数据；
		param.clear();
		param.put("subIndustry",51);
		param.put("groupId", Long.parseLong(groupId));
		param.put("distance", distance);
		param.put("user_longitude", Double.parseDouble(user_longitude));
		param.put("user_latitude", Double.parseDouble(user_latitude));
		System.out.print("参数:"+JSONObject.toJSONString(param));
		List<Map> shops = shopMapper.selectDiscountCarShopMap(param);
		List<Map<String, Object>> shopList = new ArrayList<Map<String, Object>>();
		if(shops == null) {
			return map;
		} else {
			for (Map shop : shops) {
				Map<String, Object> shopDetail = new HashMap<String, Object>();

				shopDetail.put("shop_id", shop.get("id").toString());
				shopDetail.put("shop_name", shop.get("merchant_name").toString());
				shopDetail.put("address", shop.get("address").toString());
				shopDetail.put("address_province", shop.get("address_province").toString());
				shopDetail.put("address_city", shop.get("address_city").toString());
				shopDetail.put("address_district", shop.get("address_district").toString());

				//tx转百度 返回回去
				Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.get("latitude").toString()), Double.parseDouble(shop.get("longitude").toString()));
				//坐标转换
				double longitude2 = Double.parseDouble(stringObjectMap2.get("toLongitude").toString());
				double latitude2 = Double.parseDouble(stringObjectMap2.get("toLatitude").toString());
				shopDetail.put("tolatitude", latitude2);
				shopDetail.put("tolongitude", longitude2);

				shopDetail.put("shop_distance", shop.get("distance").toString());
				shopList.add(shopDetail);
			}
		}

		map.put("shop_list", shopList);
		return map;
	}


	

	private void finishgetDistance(List<ShopDetailBean> shopDetailBeans,String longitude,String latitude) {
		long calDistance=-1;
		ArrayList<String> tolatitudes=new ArrayList<String>(); 
		ArrayList<String> tolongitudes=new ArrayList<String>(); 
		for(ShopDetailBean ShopDetailBean: shopDetailBeans){
			  
			String tolatitude=ShopDetailBean.getTxTolatitude();
			String tolongitude=ShopDetailBean.getTxTolongitude();
			if( StringUtil.notEmpty(tolatitude) && StringUtil.notEmpty(tolongitude)){
				tolatitudes.add(tolatitude);
				tolongitudes.add(tolongitude);					
				}else{
					ShopDetailBean.setShop_location(calDistance);
				}
			}		
		try {
			JSONArray result = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,latitude,longitude,tolatitudes,tolongitudes);
			if(result==null){
				for(ShopDetailBean ShopDetailBean: shopDetailBeans){
					String tolatitude=ShopDetailBean.getTxTolatitude();
					String tolongitude=ShopDetailBean.getTxTolongitude();
					double distance2= LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
					ShopDetailBean.setShop_location((long)distance2);
				}	
			}else{
				int index=0;
				for(ShopDetailBean ShopDetailBean: shopDetailBeans){
					String tolatitude=ShopDetailBean.getTxTolatitude();
					String tolongitude=ShopDetailBean.getTxTolongitude();
					if( StringUtil.notEmpty(tolatitude) && StringUtil.notEmpty(tolongitude)){
						 JSONObject elementObject=result.getJSONObject(index);
						long distance= elementObject.getLong("distance");
						 if(distance>0){
							 ShopDetailBean.setShop_location(distance);
						 }else{
							 double distance2= LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
							 ShopDetailBean.setShop_location((long)distance2);
						 }
						
						index++;
					}
					
				}
			}
		} catch (Exception e) {		
			e.printStackTrace();
		}
		
		
	}

	private Map<String, Object> setCarShopType(String name, int value, String content, int count){
		Map<String, Object> typeDetail = new HashMap<>();
		typeDetail.put("text", name);
		typeDetail.put("value", value);
		typeDetail.put("cotent", content);
		typeDetail.put("count", count);
		return typeDetail;
	}


	@Override
	public Map<String, Object> getCarShopDiscountDetail(String groupId, String memberId, String user_longitude, String user_latitude, String shopId) throws Exception {
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(shopId)) {
			throw new ApplicationException(Rescode.FAIL,"shopId 不能为空！");
		}

		//传进来的经纬度转做转换
		Map<String, Object> stringObjectMap = new HashMap<>();
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)) {
			//坐标转换
		 stringObjectMap = QqMapApiHelper.Convert_BD09_To_GCJ02(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//取得门店信息
		Shop shop = shopMapper.selectDiscountCarShopDetail(Long.parseLong(shopId));
		map.put("shop_id",shop.getId());
//		map.put("shop_ico",shop.getQrImg());
		map.put("shop_photos",shop.getAlbumpics());
		map.put("shop_name",shop.getMerchantName());
		if(shop.getShopScore() !=null){
			BigDecimal b = new   BigDecimal(shop.getShopScore());
			float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
			map.put("shop_score",f1);
		}else{
			map.put("shop_score",5);
		}
		map.put("shop_type",shop.getShopType());


		//tx转百度 返回回去
//		Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLongitude()),Double.parseDouble(shop.getLatitude()));
		Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLatitude()),Double.parseDouble(shop.getLongitude()));
		//坐标转换
		double longitude2=Double.parseDouble(stringObjectMap2.get("toLongitude").toString());
		double latitude2=Double.parseDouble(stringObjectMap2.get("toLatitude").toString());
		map.put("tolatitude",latitude2);
		map.put("tolongitude",longitude2);

		/**
		 * 动态返回给前端
		 */
		List<Map<String,Object>> typeList = new ArrayList<Map<String, Object>>();


		if (shop.getIsInstall()!=null && shop.getIsInstall() == 1 && shop.getInstallText()!=null ) {
			typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Install.getName(), shop.getIsInstall(), shop.getInstallText(), shop.getInstallText().split(",").length));
		}
		if (shop.getIsRefit()!=null && shop.getIsRefit() == 1 && shop.getRefitText()!=null){
			typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Refit.getName(), shop.getIsRefit(),shop.getRefitText(),shop.getRefitText().split(",").length));
		}
		if (shop.getIsCosmetology()!=null && shop.getIsCosmetology() == 1 && shop.getCosmetologyText()!=null){
			typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Cosmetology.getName(), shop.getIsCosmetology(),shop.getCosmetologyText(),shop.getCosmetologyText().split(",").length));
		}
		if (shop.getIsRepair()!=null && shop.getIsRepair() == 1 && shop.getRepairText()!=null){
			typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Repair.getName(), shop.getIsRepair(),shop.getRepairText(),shop.getRepairText().split(",").length));
		}
		if (shop.getIsMaintain()!=null &&shop.getIsMaintain() == 1 && shop.getMaintainText()!=null){
			typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Maintain.getName(), shop.getIsMaintain(),shop.getMaintainText(),shop.getMaintainText().split(",").length));
		}
		if(typeList.size()>0){
			map.put("type_list", typeList);
		}else{
			map.put("type_list", null);
		}

		//距离计算

		String tolongitude = shop.getLongitude();
		String tolatitude = shop.getLatitude();
		long calDistance = -1;

		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
				&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){

			//坐标转换tring
			String longitude=stringObjectMap.get("toLongitude").toString();
			String latitude=stringObjectMap.get("toLatitude").toString();

				calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,latitude,longitude,tolatitude,tolongitude);
			//如果计算距离小于要求距离不选择
//				if(distance !=null && calDistance > distance)
//					continue;
			if(calDistance>0){
				map.put("shop_location",calDistance);
			}else{
				double distance2 = LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
				map.put("shop_location",(long)distance2);
//				map.put("shop_location", Long.MAX_VALUE);
			}
		}else{
			map.put("shop_location",calDistance);
		}

		map.put("address_province",shop.getAddressProvince());
		map.put("address_city",shop.getAddressCity());
		map.put("address_district",shop.getAddressDistrict());
		map.put("shop_address",shop.getAddress());
		map.put("shop_phone",shop.getPhone());
		map.put("work_time",shop.getWorkTime());
		map.put("shop_meno",shop.getMemo());


		//取得车门店评价  显示2条必须是显示的
		Map<String,Object> param=new HashMap<String,Object>();
		param.clear();
		param.put("shopId", Long.parseLong(shopId));
		param.put("memberId", Long.parseLong(memberId));
		param.put("commentType",BizEnum.CommentType.Shop.getType());
		param.put("auditStatus",1);
		param.put("goodId", null);
		param.put("start", 0);
		param.put("limit", 2);//取2行
		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectCarShopOrGoodComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			if(mallGoodComment.getAuditStatus()==1){
				goodCommentMap.put("comment_id",mallGoodComment.getId());
				if(mallGoodComment.getIsCryptonym()){
					goodCommentMap.put("member_icon","");
					goodCommentMap.put("member_name","匿名用户");
				}
				else{
					goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
					goodCommentMap.put("member_name",mallGoodComment.getMemberName());
				}
				if(mallGoodComment.getCommentLevel()!=null){
					goodCommentMap.put("score",mallGoodComment.getCommentLevel());
				}else{
					goodCommentMap.put("score",5);
				}
				goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfDayC));
				goodCommentMap.put("comment_content",mallGoodComment.getContent());
//			String[] pics = new String[5];
				if(mallGoodComment.getPicUrls()!=null){

					String[] pics = mallGoodComment.getPicUrls().split(",");
					for (int i=0;i<pics.length;i++){
						String keyStr = "comment_imagepath"+i;
						goodCommentMap.put(keyStr,pics[i]);
					}
				}
				commentList.add(goodCommentMap);
			}

		}

		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}

		return map;
	}

	@Override
	public Map<String, Object> carShopDetail(String groupId, String memberId, String user_longitude, String user_latitude, String shopId) throws Exception {
			if (Tools.isEmpty(groupId)) {
				throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
			}
			if (Tools.isEmpty(memberId)) {
				throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
			}
			if (Tools.isEmpty(shopId)) {
				throw new ApplicationException(Rescode.FAIL,"shopId 不能为空！");
			}

			Map<String, Object> stringObjectMap = new HashMap<>();
			if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)){
				//坐标转换(百度转tx)
				 stringObjectMap = QqMapApiHelper.Convert_BD09_To_GCJ02(Double.parseDouble(user_latitude),Double.parseDouble(user_longitude));
			}

			Map<String, Object> map = new HashMap<String, Object>();
			//取得门店信息
			Shop shop = shopMapper.selectDiscountCarShopDetail(Long.parseLong(shopId));
			map.put("shop_id",shop.getId());
			if(shop.getAlbumpics() !=null){
				String[] photos = shop.getAlbumpics().split(",");
				for(int i = 0; i < photos.length; i++){
					map.put("shop_ico",photos[0]);

				}
				map.put("shop_photos",shop.getAlbumpics());
			}


			map.put("shop_name",shop.getMerchantName());
			if(shop.getShopScore() !=null){
				map.put("shop_score",Float.parseFloat(shop.getShopScore()));
			}else{
				map.put("shop_score",5);
			}
			map.put("shop_type",shop.getShopType());

			//tx转百度
			Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLatitude()),Double.parseDouble(shop.getLongitude()));
//			Map<String, Object> stringObjectMap2 = QqMapApiHelper.Convert_GCJ02_To_BD09(Double.parseDouble(shop.getLongitude()),Double.parseDouble(shop.getLatitude()));
			//坐标转换
			double longitude2=Double.parseDouble(stringObjectMap2.get("toLongitude").toString());
			double latitude2=Double.parseDouble(stringObjectMap2.get("toLatitude").toString());
			map.put("tolatitude",latitude2);
			map.put("tolongitude",longitude2);

//			map.put("tolatitude",Double.parseDouble(shop.getLongitude()));
//			map.put("tolongitude",Double.parseDouble(shop.getLatitude()));
			map.put("is_pos",shop.getCashierModePos());
			map.put("is_scan",shop.getCashierModeScan());
			map.put("is_deskcard",shop.getCashierModeDeskcard());

			/**
			 * 动态返回给前端
			 */
			List<Map<String,Object>> typeList = new ArrayList<Map<String, Object>>();

			if (shop.getIsInstall()!=null && shop.getIsInstall() == 1 && shop.getInstallText()!=null ) {
				typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Install.getName(), shop.getIsInstall(), shop.getInstallText(), shop.getInstallText().split(",").length));
			}
			if (shop.getIsRefit()!=null && shop.getIsRefit() == 1 && shop.getRefitText()!=null){
				typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Refit.getName(), shop.getIsRefit(),shop.getRefitText(),shop.getRefitText().split(",").length));
			}
			if (shop.getIsCosmetology()!=null && shop.getIsCosmetology() == 1 && shop.getCosmetologyText()!=null){
				typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Cosmetology.getName(), shop.getIsCosmetology(),shop.getCosmetologyText(),shop.getCosmetologyText().split(",").length));
			}
			if (shop.getIsRepair()!=null && shop.getIsRepair() == 1 && shop.getRepairText()!=null){
				typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Repair.getName(), shop.getIsRepair(),shop.getRepairText(),shop.getRepairText().split(",").length));
			}
			if (shop.getIsMaintain()!=null &&shop.getIsMaintain() == 1 && shop.getMaintainText()!=null){
				typeList.add(setCarShopType(YunStreamEnum.YsServiceType.Maintain.getName(), shop.getIsMaintain(),shop.getMaintainText(),shop.getMaintainText().split(",").length));
			}
			if(typeList.size()>0){
				map.put("type_list", typeList);
			}else{
				map.put("type_list", null);
			}

			//距离计算
			String tolongitude = shop.getLongitude();
			String tolatitude = shop.getLatitude();
			long calDistance = -1;
		if(StringUtil.notEmpty(user_longitude) && StringUtil.notEmpty(user_latitude)
				&& StringUtil.notEmpty(tolongitude)&& StringUtil.notEmpty(tolatitude)){

			//坐标转换tring
			String longitude=stringObjectMap.get("toLongitude").toString();
			String latitude=stringObjectMap.get("toLatitude").toString();

				calDistance = QqMapApiHelper.getDistance(QqMapApiHelper.MODE_DRIVING,latitude,longitude,tolatitude,tolongitude);
			//如果计算距离小于要求距离不选择
//				if(distance !=null && calDistance > distance)
//					continue;
			if(calDistance>0){
				map.put("shop_location",calDistance);
			}else{
				double distance2 = LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(tolatitude), Double.parseDouble(tolongitude));
				map.put("shop_location",(long)distance2);
//				map.put("shop_location", Long.MAX_VALUE);
			}
		}else{
			map.put("shop_location",calDistance);
		}

			map.put("address_province",shop.getAddressProvince());
			map.put("address_city",shop.getAddressCity());
			map.put("address_district",shop.getAddressDistrict());
			map.put("shop_address",shop.getAddress());
			map.put("shop_phone",shop.getPhone());
			map.put("work_time",shop.getWorkTime());
			map.put("shop_meno",shop.getMemo());


			//取得门店评价列表
			Map<String,Object> param=new HashMap<String,Object>();
			param.clear();
		    param.put("groupId", Long.parseLong(groupId));
			param.put("shopId", Long.parseLong(shopId));
			param.put("goodId", null);
			param.put("start", 0);
			param.put("limit", 2);//取2行
			int count = mallGoodCommentMapper.selectShopOrGoodCommentCount(param);
			map.put("comment_num",count);


			return map;
	}

	@Override
	public Map<String, Object> findAllTrandList(String memberId, String datefrom,String dateto, String type,String memo,Integer app_type, Integer pageNo, Integer pageSize) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(datefrom)) {
			throw new ApplicationException(Rescode.FAIL,"datefrom 不能为空！");
		}
		if (Tools.isEmpty(dateto)) {
			throw new ApplicationException(Rescode.FAIL,"dateto 不能为空！");
		}

		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		HashMap<String,Object> param=new HashMap<String,Object>();

		param.put("memberId", Long.parseLong(memberId));
		param.put("datefrom", datefrom);
		param.put("dateto", dateto);
		param.put("memo", memo);
		param.put("appType",app_type);

//
		if(!Tools.isEmpty(type)){
			param.put("type", type);
		}
		param.put("start", (pageNo -1)*pageSize);
		param.put("limit", pageSize);

		if(app_type ==null){//app_type 主要用来区分app 和 云流(app传 1 云流不需要传)
			TradeOffline trade = tradeOfflineMapper.selectTotalAmount(param);
			if (trade !=null){
				map.put("outComing",trade.getOutComing());
				map.put("inComing",trade.getInComing());
			}
		}
		List<TradeOffline> tradeOfflines = tradeOfflineMapper.queryTradeList(param);
		if (tradeOfflines == null || tradeOfflines.size()==0){
			return map;
		}

		List<Map<String,Object>> tradeOfflineList = new ArrayList<Map<String, Object>>();
		for(TradeOffline tradeOffline:tradeOfflines){
			Map<String, Object> tradeMap = new HashMap<String, Object>();
			if(app_type ==null){

				if(StringUtils.isNotEmpty(tradeOffline.getPayAccount())){
					MemberCard mem = memberCardMapper.querycardName(tradeOffline.getPayAccount());
					if(mem!=null) {
						tradeMap.put("cardName", mem.getIssuerBankName());
					}
				}else{
					tradeMap.put("cardName",null);
				}

				if(tradeOffline.getTransactionType()== BizEnum.TransactionType.Profit.getType()
						|| tradeOffline.getTransactionType()==BizEnum.TransactionType.RedPackage.getType()){
					tradeMap.put("payerName",tradeOffline.getReceiverName());
					tradeMap.put("payerPic",tradeOffline.getReceiverPic());
				}
				if(tradeOffline.getPayerId() !=null){

					if(tradeOffline.getPayerId().equals(memberId)){
						tradeMap.put("payerName",tradeOffline.getReceiverName());
						tradeMap.put("payerPic",tradeOffline.getReceiverPic());
					}else{
						tradeMap.put("payerPic",tradeOffline.getPayerPic());
						tradeMap.put("payerName",tradeOffline.getPayerName());
					}
				}
			}

			tradeMap.put("userId",tradeOffline.getPayerId());
			tradeMap.put("memberPhone",tradeOffline.getMemberPhone());
			tradeMap.put("order_id",tradeOffline.getOrderId());
			tradeMap.put("type",tradeOffline.getTransactionType());
			tradeMap.put("time",DateUtil.fomatDate(tradeOffline.getGmtCreate(),DateUtil.sdfTime));
			tradeMap.put("amount",tradeOffline.getRealAmount());
			tradeMap.put("status",tradeOffline.getTransactionStatus());
			tradeOfflineList.add(tradeMap);
		}
		if(tradeOfflineList.size()>0) {
			map.put("tradeOffline_list", tradeOfflineList);
		}

		return map;

	}

	@Override
	public Map<String, Object> getOrderDetail(String memberId, String orderId) throws Exception{

		Map<String, Object> map = new HashMap<String, Object>();
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(orderId)) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		Map<String,Object> param=new HashMap<String,Object>();
		param.put("memberId",Long.parseLong(memberId));
		param.put("orderId",orderId);

		TradeOffline tradeOfflines = tradeOfflineMapper.getOrderDetail(param);
		map.put("time",DateUtil.fomatDate(tradeOfflines.getGmtCreate(),DateUtil.sdfTime));
		map.put("orderNum",tradeOfflines.getOrderId());
		map.put("userName",tradeOfflines.getMemberName());
		map.put("userId",tradeOfflines.getMemberId());
		map.put("ICC",tradeOfflines.getPayOrderid());

		map.put("type",tradeOfflines.getTransactionType());
		map.put("amount",tradeOfflines.getOrderAmount());
		map.put("status",tradeOfflines.getTransactionStatus());
		map.put("payType",tradeOfflines.getPayType());

		return map;
	}

	@Override
	public Map<String, Object> cityList(String groupId, String memberId) throws Exception {
		Map<String,Object> map = new HashMap<>();

		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("groupId",Long.parseLong(groupId));
		List<Agent> firstCityList = agentMapper.queryRedCity(param);

		List<Map<String,Object>> redList = new ArrayList<Map<String, Object>>();
		for(Agent firstCity:firstCityList){
			Map<String, Object> tradeMap = new HashMap<String, Object>();
			tradeMap.put("hotCity",firstCity.getAddressCity());
			redList.add(tradeMap);
		}

		List<Agent> CityList = agentMapper.queryCity(param);
//		List<String> CityList =shopMapper.queryShopCity(Long.parseLong(groupId));
		List<Map<String,Object>> cityList = new ArrayList<Map<String, Object>>();
//		for(String firstCity:CityList){
//			Map<String, Object> cityMap = new HashMap<String, Object>();
//			cityMap.put("city",firstCity);
//			cityList.add(cityMap);
//		}
		for(Agent firstCity:CityList){
			Map<String, Object> cityMap = new HashMap<String, Object>();
			cityMap.put("city",firstCity.getAddressCity());
			cityList.add(cityMap);
		}



		if(redList.size()>0) {
			map.put("hotCity", redList);
		}
		if(cityList.size()>0) {
			map.put("city", cityList);
		}
		return map;
	}

	@Override
	public Map<String, Object> allCommentList(String groupId, String memberId,String ShopId, String score,Integer pageNo,Integer pageSize)throws Exception  {

		Map<String,Object> map = new HashMap<>();

		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(groupId)) {
			throw new ApplicationException(Rescode.FAIL,"groupId 不能为空！");
		}

		if (pageNo == null) {
			throw new ApplicationException(Rescode.FAIL,"pageNo 不能为空！");
		}
		if (pageSize == null) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		Map<String,Object> param=new HashMap<String,Object>();
		param.put("groupId",Long.parseLong(groupId));
		param.put("shopId",Long.parseLong(ShopId));
		if(score !=null){
			param.put("score",Long.parseLong(score));
		}
		param.put("start", (pageNo -1)*pageSize);
		param.put("limit", pageSize);
		param.put("commentType",BizEnum.CommentType.Shop.getType());

		List<MallGoodComment> mallGoodComments = mallGoodCommentMapper.selectAllComment(param);
		List<Map<String,Object>> commentList = new ArrayList<Map<String, Object>>();
		for(MallGoodComment mallGoodComment:mallGoodComments){
			Map<String, Object> goodCommentMap = new HashMap<String, Object>();
			if(mallGoodComment.getAuditStatus()==1){
				goodCommentMap.put("comment_id",mallGoodComment.getId());
				if(mallGoodComment.getIsCryptonym()){
					goodCommentMap.put("member_icon","");
					goodCommentMap.put("member_name","匿名用户");
				}
				else{
					goodCommentMap.put("member_icon",mallGoodComment.getMemberIcon());
					goodCommentMap.put("member_name",mallGoodComment.getMemberName());
				}
				goodCommentMap.put("score",mallGoodComment.getCommentLevel());
				goodCommentMap.put("comment_date",DateUtil.fomatDate(mallGoodComment.getGmtCreate(),DateUtil.sdfDayC));
				goodCommentMap.put("comment_content",mallGoodComment.getContent());

				if(mallGoodComment.getPicUrls() !=null){
					String[] pics = mallGoodComment.getPicUrls().split(",");
					for (int i=0;i<pics.length;i++){
						String keyStr = "comment_imagepath"+i;

						goodCommentMap.put(keyStr,pics[i]);
					}
				}
				commentList.add(goodCommentMap);
			}


		}

		if(commentList.size()>0) {
			map.put("comment_list", commentList);
		}

		return map;

	}

	@Override
	public Map<String, Object> getTransOrderDetail(String memberId, String orderId,String transactionType) throws ApplicationException {

		Map<String, Object> map = new HashMap<String, Object>();
		if (Tools.isEmpty(memberId)) {
			throw new ApplicationException(Rescode.FAIL,"memberId 不能为空！");
		}
		if (Tools.isEmpty(orderId)) {
			throw new ApplicationException(Rescode.FAIL,"pageSize 不能为空！");
		}

		Map<String,Object> param=new HashMap<String,Object>();
		param.put("memberId",Long.parseLong(memberId));
		param.put("orderId",orderId);
		param.put("transactionType",transactionType);

		Map<String,Object> resultMap = tradeOfflineMapper.getTransOrderDetail(param);
		if (resultMap.size()>0){
			if((orderId.startsWith(Const.CONSUMPTION_VIRTUAL))||(orderId.startsWith(Const.POS_DEVICE))){
				List<Map<String, Object>> coupons = tradeOfflineMapper.selectOfflineCouponsById_2((Long)resultMap.get("id"));
				resultMap.put("coupons",coupons);
				List<Map<String, Object>> usedcoupons = tradeOfflineMapper.selectOfflineUsedCouponsById_2((Long)resultMap.get("id"));
				resultMap.put("usedcoupons",usedcoupons);
			}else if(orderId.startsWith(Const.CONSUMPTION_REAL)){
				List<Map<String, Object>> coupons = tradeOfflineMapper.selectOfflineCouponsById((Long)resultMap.get("t_order_good_id"));
				resultMap.put("coupons",coupons);
				List<Map<String, Object>> usedcoupons = tradeOfflineMapper.selectOfflineUsedCouponsById((Long)resultMap.get("t_order_good_id"));
				resultMap.put("usedcoupons",usedcoupons);
			}else{
				List<Map<String, Object>> coupons = tradeOfflineMapper.selectOfflineCouponsById((Long)resultMap.get("id"));
				resultMap.put("coupons",coupons);
				List<Map<String, Object>> usedcoupons = tradeOfflineMapper.selectOfflineUsedCouponsById((Long)resultMap.get("id"));
				resultMap.put("usedcoupons",usedcoupons);
			}

		}
		/*TradeOffline tradeOffline1 = tradeOfflineMapper.selectOfflineBackAmtById((Long)resultMap.get("id"));
		if (tradeOffline1 != null){
			resultMap.put("back_amount",tradeOffline1.getOrderAmount());
		}*/
		TradeOffline tradeOffline1  = tradeOfflineMapper.getTradeDataByRelatedTradeOfflineId((Long)resultMap.get("id"),memberId);
//        TradeOffline tradeOffline1 = tradeOfflineMapper.selectOfflineBackAmtById(offline.getId());

		if (tradeOffline1 != null){
			resultMap.put("back_amount",tradeOffline1.getOrderAmount());
		}




		return resultMap;
	}
}

